import _objectSpread from '@babel/runtime/helpers/esm/objectSpread2';
import _objectWithoutProperties from '@babel/runtime/helpers/esm/objectWithoutProperties';
import { Transfer as AntdTransfer, Table } from 'antd';
import difference from 'lodash/difference';
import React from 'react';
import createField from '../../../createField';
var _excluded = ['leftColumns', 'rightColumns', 'children', 'tableProps'];
var TableTransfer = function TableTransfer(_ref) {
  var leftColumns = _ref.leftColumns,
    rightColumns = _ref.rightColumns,
    children = _ref.children,
    _ref$tableProps = _ref.tableProps,
    tableProps = _ref$tableProps === void 0 ? {} : _ref$tableProps,
    rest = _objectWithoutProperties(_ref, _excluded);
  return /*#__PURE__*/ React.createElement(AntdTransfer, _objectSpread({}, rest), function (_ref2) {
    var direction = _ref2.direction,
      filteredItems = _ref2.filteredItems,
      onItemSelectAll = _ref2.onItemSelectAll,
      onItemSelect = _ref2.onItemSelect,
      listSelectedKeys = _ref2.selectedKeys,
      listDisabled = _ref2.disabled;
    var columns = direction === 'left' ? leftColumns : rightColumns;
    var rowSelection = {
      getCheckboxProps: function getCheckboxProps(item) {
        return {
          disabled: listDisabled || item.disabled,
        };
      },
      onSelectAll: function onSelectAll(selected, selectedRows) {
        var treeSelectedKeys = selectedRows
          .filter(function (item) {
            return !item.disabled;
          })
          .map(function (_ref3) {
            var key = _ref3.key;
            return key;
          });
        var diffKeys = selected
          ? difference(treeSelectedKeys, listSelectedKeys)
          : difference(listSelectedKeys, treeSelectedKeys);
        onItemSelectAll(diffKeys, selected);
      },
      onSelect: function onSelect(_ref4, selected) {
        var key = _ref4.key;
        onItemSelect(key, selected);
      },
      selectedRowKeys: listSelectedKeys,
    };
    return /*#__PURE__*/ React.createElement(
      Table,
      _objectSpread(
        _objectSpread(
          {
            rowSelection: rowSelection,
            dataSource: filteredItems,
            size: 'small',
            // @ts-ignore
            style: {
              pointerEvents: listDisabled ? 'none' : null,
            },
          },
          tableProps,
        ),
        {},
        {
          // @ts-ignore
          columns: columns,
          // @ts-ignore
          onRow: function onRow(_ref5) {
            var key = _ref5.key,
              itemDisabled = _ref5.disabled;
            return {
              onClick: function onClick() {
                if (itemDisabled || listDisabled) return;
                onItemSelect(key, !listSelectedKeys.includes(key));
              },
            };
          },
        },
      ),
    );
  });
};
export default createField(TableTransfer, {
  defaultFormItemProps: {
    valuePropName: 'targetKeys',
  },
});
